package com.example.howard.mytcpclient;

import android.app.Activity;
import android.app.Application;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.fastjson.JSONObject;

import java.lang.ref.WeakReference;


public class ConnectActivity extends Activity {
    //定义UI按键
    private EditText eip;
    private EditText eport;
    private Button econnect;
    private com.example.howard.mytcpclient.ConnectService conService;
//    private MyApplication mapp;
//    private KeyHandler keyHandler;
//    private MainActivity.MyHandler changkeyHandler;
    //设置连接按钮的名称
    private static final String BTN_TAG_CONNECT ="Conncet";
    private static final String BTN_TAG_DISCONNECT = "Disconncet";
    private final String TAG = "ConnectActivity";
//    private String keyname="";
//    private static final int DIS=2234;

    private MyApplication myApplication;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.connect);

        myApplication = (MyApplication)getApplication();
        //引用共享Handler
//        mapp = (MyApplication)getApplication();
//        keyHandler = new KeyHandler();
//        mapp.setKeyHandler(keyHandler);
//        changkeyHandler = mapp.getChangkeyHandler(changkeyHandler);
    }

    //初始化视图及内容
    private void initView() {
        eip = (EditText) findViewById(R.id.ip);
        eport = (EditText) findViewById(R.id.port);
        econnect = (Button) findViewById(R.id.connect);
        if(myApplication.keyname){
            econnect.setText(BTN_TAG_CONNECT);
        }else
        econnect.setText(BTN_TAG_DISCONNECT);
        eip.setText(R.string.edit_ip);
        eport.setText(R.string.edit_port);
    }
//    private final static int CONNECT=1234;
    //连接按键的功能及名称设置
    private void initControl() {
        econnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (conService.isconnect()) {
                    conService.clientThread.showdisconncet();
                    conService.disconnect();
                    econnect.setText(BTN_TAG_CONNECT);
                    myApplication.keyname=true;
                    myApplication.IP=eip.getText().toString();
                    myApplication.Port=eport.getText().toString();

                } else {
                    String ip = eip.getText().toString().trim();
                    String port = eport.getText().toString().trim();
                    conService.connect(ip, port);
                    econnect.setText(BTN_TAG_DISCONNECT);
                    myApplication.keyname=false;
                    myApplication.IP=eip.getText().toString();
                    myApplication.Port=eport.getText().toString();
//                    Message message =new Message();
//                    message.what=CONNECT;
//                    message.obj=BTN_TAG_DISCONNECT;
//                    changkeyHandler.sendMessage(message);
                }
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        initView();
        initControl();
        bindService(new Intent(this, ConnectService.class), connection, BIND_AUTO_CREATE);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unbindService(connection);
    }

    //绑定服务的相关设置
    ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder binder) {
            conService = ((ConnectService.ConnectBinder) binder).getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };
//    private static final int KEY=2234;
//    //自定义KeyHandler
//    public class KeyHandler extends Handler {
//
//        @Override
//        public void handleMessage(Message msg) {
//            ConnectActivity theActivity = new ConnectActivity();
//            switch (msg.what) {
//
////                case KEY:
////                   theActivity.keyname= (String) msg.obj;
////                   theActivity.econnect.setText(keyname);
////                   break;
//            }
//            super.handleMessage(msg);
//        }
//    }
}
